package xiangmu

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
	"zhuandan/data_source"
	"zhuandan/models"
)

type FupantiyisRBody struct {
	Data       []models.Fupaninfo `json:"data"`
	PageNo     int                `json:"pageNo"`
	PageSize   int                `json:"pageSize"`
	TotalCount int64              `json:"totalCount"`
	TotalPage  int64              `json:"totalPage"`
}

type FupantiyisBody struct {
	Page     int   `json:"pageNo" from:"pageNo"`
	Pagesize int   `json:"pageSize" from:"pageSize"`
	Fupanid  int64 `json:"fupanid" gorm:"fupanid"`
}

func Fupantiyis(ctx *gin.Context) {
	request := FupantiyisBody{}
	ctx.ShouldBind(&request)
	currentPage := request.Page
	fupanid := request.Fupanid
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Fupaninfo
	res := data_source.Db
	if fupanid > 0 {
		res = res.Where("fupanid = ?", fupanid)
	}
	res.Model(&models.Fupaninfo{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := FupantiyisRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}

type FupantiyiBody struct {
	ID       int64  `json:"id" gorm:"id"`
	Mubiao   string `json:"mubiao" gorm:"mubiao"`
	Jieguo   string `json:"jieguo" gorm:"jieguo"`
	Chayi    string `json:"chayi" gorm:"chayi"`
	Jianyi   string `json:"jianyi" gorm:"jianyi"`
	Xingdong string `json:"xingdong" gorm:"xingdong"`
	Zeren    string `json:"zeren" gorm:"zeren"`
	Zerenid  int64  `json:"zerenid" gorm:"zerenid"`
	Wctime   string `json:"wctime" gorm:"wctime"`
	Jingyan  string `json:"jingyan" gorm:"jingyan"`
	Tiyi     string `json:"tiyi" gorm:"tiyi"`
	Tiyiid   int64  `json:"tiyiid" gorm:"tiyiid"`
	Fupanid  int64  `json:"fupanid" gorm:"fupanid"`
	Adminid  int64  `json:"adminid" gorm:"adminid"`
}

func Fupantiyiadd(ctx *gin.Context) {
	request := FupantiyiBody{}
	ctx.ShouldBind(&request)
	mubiao := request.Mubiao
	jieguo := request.Jieguo
	jianyi := request.Jianyi
	adminid := request.Adminid
	chayi := request.Chayi
	xingdong := request.Xingdong
	zeren := request.Zeren
	zerenid := request.Zerenid
	wctime := request.Wctime
	jingyan := request.Jingyan
	tiyi := request.Tiyi
	tiyiid := request.Tiyiid
	fupanid := request.Fupanid

	huiyiadd := models.Fupaninfo{
		Mubiao:   mubiao,
		Jieguo:   jieguo,
		Chayi:    chayi,
		Xingdong: xingdong,
		Zeren:    zeren,
		Zerenid:  zerenid,
		Wctime:   wctime,
		Jingyan:  jingyan,
		Tiyi:     tiyi,
		Tiyiid:   tiyiid,
		Fupanid:  fupanid,
		Jianyi:   jianyi,
	}
	data_source.Db.Create(&huiyiadd)
	fupanlog := models.Fupanlog{
		Fupaninfoid: huiyiadd.ID,
		Adminid:     adminid,
		Addtime:     time.Now().Unix(),
		Title:       "添加复盘提议",
	}
	data_source.Db.Create(&fupanlog)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

func Fupantiyiupdate(ctx *gin.Context) {
	request := FupantiyiBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	mubiao := request.Mubiao
	jieguo := request.Jieguo
	adminid := request.Adminid
	chayi := request.Chayi
	xingdong := request.Xingdong
	zeren := request.Zeren
	zerenid := request.Zerenid
	wctime := request.Wctime
	jingyan := request.Jingyan
	tiyi := request.Tiyi
	jianyi := request.Jianyi
	tiyiid := request.Tiyiid
	fupanid := request.Fupanid

	huiyiadd := models.Fupaninfo{
		Mubiao:   mubiao,
		Jieguo:   jieguo,
		Chayi:    chayi,
		Xingdong: xingdong,
		Zeren:    zeren,
		Zerenid:  zerenid,
		Wctime:   wctime,
		Jingyan:  jingyan,
		Tiyi:     tiyi,
		Tiyiid:   tiyiid,
		Fupanid:  fupanid,
		Jianyi:   jianyi,
	}
	data_source.Db.Where("id = ?", id).Updates(&huiyiadd)
	fupanlog := models.Fupanlog{
		Fupaninfoid: huiyiadd.ID,
		Adminid:     adminid,
		Addtime:     time.Now().Unix(),
		Title:       "修改复盘提议",
	}
	data_source.Db.Create(&fupanlog)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

func Fupantiyidel(ctx *gin.Context) {
	request := FupanBody{}
	ctx.ShouldBind(&request)
	id := request.ID

	data_source.Db.Where("id = ?", id).Delete(&models.Fupaninfo{})

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

type FupantiyilogsRBody struct {
	Data       []models.Fupanlog `json:"data"`
	PageNo     int               `json:"pageNo"`
	PageSize   int               `json:"pageSize"`
	TotalCount int64             `json:"totalCount"`
	TotalPage  int64             `json:"totalPage"`
}

type FupantiyilogsBody struct {
	Page        int   `json:"pageNo" from:"pageNo"`
	Pagesize    int   `json:"pageSize" from:"pageSize"`
	Fupantiyiid int64 `json:"fupantiyiid" gorm:"fupantiyiid"`
}

func Fupantiyilogs(ctx *gin.Context) {
	request := FupantiyilogsBody{}
	ctx.ShouldBind(&request)
	currentPage := request.Page
	fupantiyiid := request.Fupantiyiid
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Fupanlog
	res := data_source.Db.Where("fupaninfoid = ?", fupantiyiid)
	res.Model(&models.Fupanlog{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := FupantiyilogsRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}
